package com.menker.app.rest;

import com.menker.app.Constants.API;
import com.menker.app.biz.PageListInfo;
import com.menker.app.common.SharedPreferencesHelper;
import com.menker.app.rest.result.ApiResult;
import com.menker.app.rest.result.AppInfo;
import com.menker.app.rest.result.ChannelInfo;
import com.menker.app.rest.result.LoginResult;
import com.menker.app.rest.result.MsgInfo;
import com.menker.app.rest.result.ShareInfo;
import com.menker.app.rest.result.UploadResult;

import org.json.JSONObject;

import java.util.List;
import java.util.Map;

import okhttp3.RequestBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class ApiHelper {

    private static final String APP_OS = "Android";

    private Api mApi;
    private SharedPreferencesHelper mSharedPreferencesHelper;
    private String mDeviceId;
    private int mVersion;

    public ApiHelper(Api api, SharedPreferencesHelper sharedPreferencesHelper, String deviceId, int version) {
        mApi = api;
        mSharedPreferencesHelper = sharedPreferencesHelper;
        mDeviceId = deviceId;
        mVersion = version;
    }

    public void login(Subscriber<ApiResult> subscriber,
                      String mobilePhone, String verifyCode, String extend) {
        mApi.login(mSharedPreferencesHelper.getString(API.KEY_TICKET, ""), APP_OS, mDeviceId,
                mobilePhone, verifyCode, extend)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void thirdLogin(Subscriber<LoginResult> subscriber,
                           String openId, String platFrom, String accessToken, String nickName, String avatar, String extend) {
        mApi.thirdLogin(mSharedPreferencesHelper.getString(API.KEY_TICKET, ""), APP_OS, mDeviceId,
                openId, platFrom, accessToken, nickName, avatar, extend)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getAllChannel(Subscriber<ChannelInfo> subscriber) {
        mApi.getAllChannel(mSharedPreferencesHelper.getString(API.KEY_TICKET, ""))
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void createOrder(Subscriber<ApiResult> subscriber,
                           String orderId) {
        mApi.createOrder(mSharedPreferencesHelper.getString(API.KEY_TICKET, ""),
                orderId)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getShareInfo(Subscriber<ShareInfo> subscriber,
                            String itemId) {
        mApi.getShareInfo(mSharedPreferencesHelper.getString(API.KEY_TICKET, ""),
                itemId)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getAppInfo(Subscriber<AppInfo> subscriber) {
        mApi.getAppInfo(mSharedPreferencesHelper.getString(API.KEY_TICKET, ""),
                APP_OS, mDeviceId, mVersion)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void uploadImg(Subscriber<UploadResult> subscriber, Map<String, RequestBody> maps, String description) {
        mApi.uploadImg(mSharedPreferencesHelper.getString(API.KEY_TICKET, ""),
                description,maps)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getMsgInfo(Subscriber<MsgInfo> subscriber) {
        mApi.getMsgInfo(mSharedPreferencesHelper.getString(API.KEY_TICKET, ""))
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getShare(Subscriber<ShareInfo> subscriber, PageListInfo pageList) {
        mApi.getShare(mSharedPreferencesHelper.getString(API.KEY_TICKET, ""), pageList)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }
}
